package com.sams.controller;

import java.io.File;
import java.io.IOException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import com.github.pagehelper.PageInfo;
import com.sams.entity.EduManage;
import com.sams.service.EduManageService;


@RequestMapping("/edumanage")
@Controller
public class EduManageController {
	
	@Autowired
	private EduManageService service;
	
	//分页查询
		@RequestMapping("/list")
		public String listEduManage(Integer currentPage,Model model) {
			if(currentPage==null || currentPage ==0) {
				currentPage=1;
			}
			PageInfo<EduManage> pageInfo= service.listEduManage(currentPage);
			model.addAttribute("pageInfo",pageInfo);
			return "edumanage";
		}
		//详情
		@RequestMapping("/findById/{id}")
		public String findById(@PathVariable("id") Integer id,Model model) {
			EduManage edu=service.findById(id);
			
			//System.out.println(edu.getActivityImg());
			String str=edu.getActivityImg().toString();
			String[] activityImg1=str.split(",");
			model.addAttribute("edu",edu);
			//String a="p1.jpg";
			model.addAttribute("p1", activityImg1[0]);
			model.addAttribute("p2", activityImg1[1]);
			model.addAttribute("p3", activityImg1[2]);
			//System.out.println(activityImg1[0]);
			
			return "edumanage_detail";
		}

		//删除领导信息
		@RequestMapping("/delete/{id}")
		public String delete(@PathVariable("id") Integer id) {
			service.delete(id);
			return "forward:/edumanage/list";
		}
		//添加
		@RequestMapping("/add")
		public String uploadAll(
				@RequestParam("term") String term ,
				@RequestParam("subject") String subject ,
				@RequestParam("type") String type ,
				@RequestParam("organization") String organization ,
				@RequestParam("scope") String scope ,
				@RequestParam("count") int count ,
				@RequestParam("activity") String activity ,
				@RequestParam("remark") String remark ,
				@RequestParam("file") MultipartFile [] myfiles) throws IllegalStateException, IOException {
			
			StringBuilder ac=new StringBuilder();
			for(MultipartFile myfile : myfiles) {
				
				File file  = new  File("E:\\img\\"+myfile.getOriginalFilename());
				ac.append(myfile.getOriginalFilename());
				ac.append(",");
				myfile.transferTo(file);
				
		   }
			
			String activityImg=ac.toString();
			EduManage edu=new EduManage();
			edu.setTerm(term);
			edu.setSubject(subject);
			edu.setType(type);
			edu.setOrganization(organization);
			edu.setScope(scope);
			edu.setCount(count);
			edu.setActivity(activity);
			edu.setActivityImg(activityImg);
			edu.setRemark(remark);
			
			service.add(edu);
			
			return "forward:/edumanage/list";
		}
		
		@RequestMapping("/update")
		public String update(
				@RequestParam("id") int id ,
				@RequestParam("term") String term ,
				@RequestParam("subject") String subject ,
				@RequestParam("type") String type ,
				@RequestParam("organization") String organization ,
				@RequestParam("scope") String scope ,
				@RequestParam("count") int count ,
				@RequestParam("activity") String activity ,
				@RequestParam("remark") String remark ,
				@RequestParam("file") MultipartFile [] myfiles) throws IllegalStateException, IOException {
			
			StringBuilder ac=new StringBuilder();
			for(MultipartFile myfile : myfiles) {
				
				File file  = new  File("E:\\img\\"+myfile.getOriginalFilename());
				ac.append(myfile.getOriginalFilename());
				ac.append(",");
				myfile.transferTo(file);
				
		   }
			
			String activityImg=ac.toString();
			EduManage edu=new EduManage();
			edu.setId(id);
			edu.setTerm(term);
			edu.setSubject(subject);
			edu.setType(type);
			edu.setOrganization(organization);
			edu.setScope(scope);
			edu.setCount(count);
			edu.setActivity(activity);
			edu.setActivityImg(activityImg);
			edu.setRemark(remark);
			
			service.update(edu);
			
			return "forward:/edumanage/list";
		}
}
